package com.db.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.DefaultListModel;

import com.data.Data;
import com.db.bean.Work;
import com.db.bean.Workclass;
import com.db.dao.WorkClassDAOImpl;
import com.db.dao.WorkDAOImpl;

/**
* @author 作者:秋哲    E-mail:2810201146@qq.com:
* @version 创建时间：2020年5月25日 下午5:01:33
* @description: 
*/
public class WorkServer {
	//把Data静态数据转换为控件支持的格式
	public DefaultListModel<String> updateDataWorkList(){
		DefaultListModel<String> list = new DefaultListModel<String>();
		List<Work> workList = Data.workList;
		for (Work work : workList) {
			list.addElement(work.getTitle());
		}
		return list;
	}
	//获取单个作业详情
	public Work getWorkDetails(int GuiIndex) {
		int workId = Data.workList.get(GuiIndex).getId();
		int index = this.DataById(workId);
		return Data.workList.get(index);
	}
	//获取作业列表  是一个work对象列表
	public List<Work> getWorkList(String GuiGroupTitle) {
		List<Work> workList = new ArrayList<Work>();
		if(GuiGroupTitle==null || "".equals(GuiGroupTitle))return workList;
		int workGroupId = Data.workgroup.get(GuiGroupTitle);
		workList = new WorkDAOImpl().getAll(workGroupId);
		Data.workList = workList;
		GroupToDri(GuiGroupTitle);
		return workList;
	}
	//生成目录
	public void GroupToDri(String GuiGroupTitle) {
		List<Work> workList = Data.workList;
		File file = new File("data/",GuiGroupTitle);
		for (Work work : workList) {
			new File(file,work.getTitle()).mkdirs();
		}
	}
	//新建作业 
	public int insertWork(String title,String msg,String GuiGroupTitle) {
		if(title.length()==0||title==null||title=="")return 0;
		int index = DataByTitle(title);
		if(index>=0)return 0;
		Integer integer = Data.workgroup.get(title);
		DataByTitle(GuiGroupTitle);
		if(integer!=null) return 0;
		int workClass_id = Data.workgroup.get(GuiGroupTitle);
		Work work_obj= new Work(1, msg, workClass_id, title);
		WorkDAOImpl workDAOImpl = new WorkDAOImpl();
		int insert=workDAOImpl.insert(work_obj);
		if(insert==1) {
			work_obj = workDAOImpl.getByTitle(workClass_id, title);
			Data.workList.add(work_obj);
		}
		return insert;
	}
	//删除作业 
	public int deleteWork(int GuiIndex) {
		int workId = Data.workList.get(GuiIndex).getId();
		int index = this.DataById(workId);
		if(index<0)return 0;
		WorkDAOImpl dao = new WorkDAOImpl();
		int n = dao.deleteById(workId);
		if(n==1) {
			Data.workList.remove(index);
		}
		return n;
	}
	//修改作业 
	public int updateWorl(String newTitle,String newMsg,int GuiIndex) {
		WorkDAOImpl dao = new WorkDAOImpl();
		int workId = Data.workList.get(GuiIndex).getId();
		int index = this.DataById(workId);
		if(index<0)return 0;
//		....Obj = Data.workList.get(index)  赋值的是地址还是内容的拷贝
		Data.workList.get(index);
		Data.workList.get(index).setTitle(newTitle);
		Data.workList.get(index).setMsg(newMsg);
		int n = dao.update(Data.workList.get(index));
		if(n==1) {
		}
		return n;
	}
	private int DataById(int workId) {
		for (int i = 0; i < Data.workList.size(); i++) {
			Work work = Data.workList.get(i);
			if(work.getId()==workId){
				return i;
			}
		}
		return -1;
	}
	private int DataByTitle(String title) {
		for (int i = 0; i < Data.workList.size(); i++) {
			Work work = Data.workList.get(i);
			if(work.getTitle().equals(title)){
				return i;
			}
		}
		return -1;
	}
}
